import { get, post, orderService } from './request'
import type { ApiResponse } from './user'

// 订单相关接口类型定义
export interface OrderItem {
  id?: number
  dishId: number
  dishSpecId?: number
  quantity: number
  dishName?: string
  dishPrice?: number
  dishImage?: string
}

export interface Order {
  id?: number
  orderNo?: string
  userId: number
  merchantId: number
  totalAmount?: number
  deliveryFee?: number
  actualAmount?: number
  deliveryAddress: string
  receiverName: string
  receiverPhone: string
  status?: number
  statusDesc?: string
  payMethod?: number
  payMethodDesc?: string
  remark?: string
  orderItems: OrderItem[]
  createTime?: string
  payTime?: string
  deliveryTime?: string
  completeTime?: string
  merchantName?: string
  userName?: string
  expectedDeliveryTime?: string
  actualDeliveryTime?: string
  updateTime?: string
}

// 分页数据结构
export interface PageData<T> {
  content: T[]
  empty: boolean
  first: boolean
  last: boolean
  number: number
  numberOfElements: number
  pageable: {
    offset: number
    pageNumber: number
    pageSize: number
    paged: boolean
    sort: any[]
    unpaged: boolean
  }
  size: number
  sort: any[]
  totalElements: number
  totalPages: number
}

// 创建订单
export function createOrder(data: Order) {
  return post<ApiResponse<Order>>(orderService, '/api/orders', data)
}

// 根据订单ID查询订单
export function getOrderById(id: number) {
  return get<ApiResponse<Order>>(orderService, `/api/orders/${id}`)
}

// 根据订单号查询订单
export function getOrderByNo(orderNo: string) {
  return get<ApiResponse<Order>>(orderService, `/api/orders/no/${orderNo}`)
}

// 根据用户ID查询订单列表
export function getUserOrders(userId: number) {
  return get<ApiResponse<Order[]>>(orderService, `/api/orders/user/${userId}`)
}

// 支付订单
export function payOrder(orderNo: string, payMethod: number) {
  return post<ApiResponse<any>>(orderService, `/api/orders/pay/${orderNo}?payMethod=${payMethod}`, {})
}

// 取消订单
export function cancelOrder(orderNo: string) {
  return post<ApiResponse<any>>(orderService, `/api/orders/cancel/${orderNo}`)
} 

// 根据商家ID查询订单列表
export function getMerchantOrders(merchantId: number) {
  return get<ApiResponse<PageData<Order>>>(orderService, `/api/orders/merchant/${merchantId}`)
}

// 商家接单
export function acceptOrder(orderNo: string) {
  return post<ApiResponse<any>>(orderService, `/api/orders/accept/${orderNo}`)
}

// 完成订单
export function completeOrder(orderNo: string) {
  return post<ApiResponse<any>>(orderService, `/api/orders/complete/${orderNo}`)
} 